fromusername = sessionStorage.getItem("username")
$(function (){
    if(sessionStorage.getItem("userid")==null){
        alert("请先登录！")
        window.location.href="login.html"
        return
    }
    sessionStorage.setItem("toid",0)
    sendBtn = $("#send")
    sendMsg = $("#sendmsg")
    showMsg = $("#showmsg")
    userlist = $("#userlist")
    $("#welcome").html("欢迎您~"+" "+sessionStorage.getItem("username"))
    $("#logout").click(function (){
        sessionStorage.clear()
        window.location.href="login.html"
    })
    $("#public").click(function (){
        sessionStorage.setItem("toid",0)
        // setInterval(querymsgall,"500")
    })
    $.ajax({
        type:"post",
        url:"http://127.0.0.1:9090/getusers",
        success:function (res){
            let s = ""
            for(let r in res){
                if(res[r].username===sessionStorage.getItem("username")){
                    continue
                }
                s += "<a href=\"#\" class=\"list-group-item list-group-item-action\">"+res[r].username+"</a>    "
            }
            userlist.append(s)
            $(".list-group-item").click(function (){
                $("#chattitle").html($(this).html())
                let toid = 0
                for(let r in res){
                    if(res[r].username===$(this).html()){
                        sessionStorage.setItem("toid",res[r].id)
                        break;
                    }
                }
            })
        },
        error:function (res){
            alert("请求错误")
        }
    })
    sendBtn.click(function(){
        let msg = sendMsg.val()
        sendMsg.val("")
        // showMsg.html(msg)
        // sendMsg.html("")
        // alert(msg)
        $.ajax({
            type:"post",
            url:"http://127.0.0.1:9090/sendmsg",
            data:{msg:msg,from:sessionStorage.getItem("userid"),to:sessionStorage.getItem("toid"),fromusername:fromusername},
            success:function (res){
                console.log(JSON.stringify(res))
            },
            error:function (res){
                alert("请求错误")
            }
        })
        scrolltobotom()
    })
    setInterval(querymsg,500)
    setTimeout("scrolltobotom()",1500)
})
function scrolltobotom()
{
    var obj = document.getElementById("showmsg");
    obj.scrollTop = obj.scrollHeight+20; // good
}
function onTextareaKeyDown(){
    if (event.keyCode === 13) { //如果按的是enter键 13是enter
        event.preventDefault(); //禁止默认事件（默认是换行）
        sendBtn.click()
    }
}
function querymsg(){
    $.ajax({
        type:"post",
        url:"http://127.0.0.1:9090/querymsg",
        data:{from:sessionStorage.getItem("userid"),to:sessionStorage.getItem("toid")},
        success:function (res){
            let msgres = ""
            for(let m in res){
                time = res[m].time.split("T")[1].split(".")[0]
                msg = res[m].msg
                username = res[m].fromusername
                msgres+=username+"  "+time+"\n"+msg+"\n"
            }
            $("#showmsg").html(msgres)
        },
        error:function (err){
            console.log(err+"错误")
        }
    })
}